﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Coolite.Ext.Web;
using CXDB;
using SubSonic;

public partial class Safty_UserListReply : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
         CXSecurity.ChkBoss(Cookies.getCookies("cuid"));
         if (!IsPostBack)
         {
             Store1Bind();
         }
    }
    protected void Store1_OnRefreshData(object s, StoreRefreshDataEventArgs e)
    {
        Store1Bind();
    }
    protected void RowSelect(object sender, AjaxEventArgs e)
    {

        string id = e.ExtraParams["ida"];
        IDLabel.Text = id;
        rID.Value = id;
        Query q = new Query(VwStaffSaftyStep1.Schema);
        q.AddWhere("id", id);
        IDataReader dr = q.ExecuteReader();
        if (dr.Read())
        {
            tfguid.Value = dr["guid"].ToString();

            string[] str = CXFileUpload.show(new Guid(dr["guid"].ToString())).Split('$');
            tffileshow.NavigateUrl = str[0];
            tffileshow.Text = str[1];

            TfUserId.Text = dr["userid"].ToString();
            TfUserName.Text = dr["EN_username"].ToString();
            TfDeptName.Text = dr["EN_Department"].ToString();
            TfAccident_time.Text = dr["Accident_time"].ToString();
            TfAccident_reason.Text = dr["Accident_reason"].ToString();
            TfDeptID.Value = dr["DeptID"].ToString();
            TfAccident_Duty.Text=dr["Accident_duty"].ToString();
            TfEntering_username.Text = dr["ED_username"].ToString(); 
             

            TfSafty_Master_Status.Reset();
            TfSafty_Master_Status.SetInitValue(dr["Safty_master_status"].ToString());
            TfSafty_master_remark.Text = dr["safty_master_remark"].ToString();

            TfDept_Master_ID.Reset();
            TfDept_Master_ID.SetInitValue(dr["dept_master_id"].ToString());
            TfDept_master_status_Comb.Reset();
            TfDept_master_status_Comb.SetInitValue(dr["dept_master_status"].ToString());
            TfDept_Master_Remark.Text = dr["dept_master_remark"].ToString();
            TfBoss_ID.Reset();
            Store3Bind();
            TfBoss_ID.SetInitValue(dr["Boss_userid"].ToString()); 
            TfBoss_Remark.Text = dr["boss_remark"].ToString();
            TfBoss_Status.Reset();
            TfBoss_Status.SetInitValue(dr["boss_status"].ToString());

            if (dr["Safty_master_status"].ToString() == dr["dept_master_status"].ToString() && dr["boss_status"].ToString() == dr["Safty_master_status"].ToString() && dr["Safty_master_status"].ToString() == "1")
            {
                Button1.Disabled = true;
            }
            else
            {
                Button1.Disabled = false;
            }
            //安监领导审批
            if ((Cookies.getCookies("cDeptID") == "6") && (Cookies.getCookies("cLevelID") == "1"))
            {
                TfSafty_Master_Status.Disabled = false;
                TfSafty_master_remark.Disabled = false;
                TfDept_Master_ID.Disabled = false;
                TfBoss_ID.Disabled = false;
            }

            //公司领导审批框
            if ((Cookies.getCookies("cDeptID") == "1") && Cookies.getCookies("cLevelID") == "1")
            {
                TfBoss_Remark.Disabled = false;
                TfBoss_Status.Disabled = false;

            }
            //部门领导审批框
            if ((Cookies.getCookies("cDeptID") == getDeptId(int.Parse(dr["deptid"].ToString())).ToString()) && Cookies.getCookies("cLevelID") == "1")
            {
                TfDept_Master_Remark.Disabled = false;
                TfDept_master_status_Comb.Disabled = false;
            }

        }
    }
    protected void SubClick(object s, AjaxEventArgs e)
    {
        if (rID.Value.ToString() == "")
        {
            Err.Warning("请从左侧列表框选择一个记录，再进行此项操作！",MessageBox.Icon.WARNING);
            return;
        }
        
            if (TfSafty_Master_Status.SelectedIndex == -1)
            {
                Err.Warning("请选择审核状态！",MessageBox.Icon.WARNING);
                return;
            }
            
                if (TfDept_Master_ID.SelectedIndex == -1)
                {
                    Err.Warning("请选择部门领导！",MessageBox.Icon.WARNING);
                    return;
                }
               
                    if (TfBoss_ID.SelectedIndex == -1)
                    {
                        Err.Warning("请选择公司领导！",MessageBox.Icon.WARNING);
                        return;
                    }
                    
                        int id = int.Parse(rID.Value.ToString());

                        
                        //记录该条操作记录
                        CxRemark cxr = new CxRemark();
                        cxr.Guid = new Guid(tfguid.Value.ToString());
                        cxr.TypeID = 1;
                        cxr.UID = int.Parse(Cookies.getCookies("cUid"));
                        if((Cookies.getCookies("cDeptID")!="6")&&(Cookies.getCookies("cDeptID")!="1")&&(Cookies.getCookies("cLevelID")=="1"))
                        {
                            if (TfDept_Master_Remark.Text.Trim() == "") { Err.Warning("审核意见不能为空！", MessageBox.Icon.WARNING); return; }
                            if (TfDept_master_status_Comb.SelectedIndex == -1) { Err.Warning("请选择审核态度！",MessageBox.Icon.WARNING); return; }
                            cxr.Remark = TfDept_Master_Remark.Text;
                            cxr.TStatus = int.Parse(TfDept_master_status_Comb.SelectedItem.Value);
                        }
                        if((Cookies.getCookies("cDeptID")=="6")&&(Cookies.getCookies("cLevelID")=="1"))
                        {
                            if (TfSafty_master_remark.Text.Trim() == "") { Err.Warning("审核意见不能为空！", MessageBox.Icon.WARNING); return; }
                            if (TfSafty_Master_Status.SelectedIndex == -1) { Err.Warning("请选择审核态度！", MessageBox.Icon.WARNING); return; }
                            cxr.Remark = TfSafty_master_remark.Text;
                            cxr.TStatus = int.Parse(TfSafty_Master_Status.SelectedItem.Value);
                        }
                        if((Cookies.getCookies("cDeptID")=="1")&&(Cookies.getCookies("cLevelID")=="1"))
                        {
                            if (TfBoss_Remark.Text.Trim() == "") { Err.Warning("审核意见不能为空！", MessageBox.Icon.WARNING); return; }
                            if (TfBoss_Status.SelectedIndex == -1) { Err.Warning("请选择审核态度！", MessageBox.Icon.WARNING); return; }

                            cxr.Remark = TfBoss_Remark.Text;
                            cxr.TStatus = int.Parse(TfBoss_Status.SelectedItem.Value);
                        }
                        cxr.InTime = DateTime.Now;
                        cxr.Status = 1;
                        cxr.Save();

                        //更新主数据库
                        CxStaffSafty cx = new CxStaffSafty("id", id);

                        //安监领导数据
                        if ((Cookies.getCookies("cDeptID") == "6") && Cookies.getCookies("cLevelID") == "1")
                        {
                            cx.SaftyMasterRemark =TfSafty_master_remark.Text;
                            cx.SaftyMasterStatus = int.Parse(TfSafty_Master_Status.SelectedItem.Value);
                            cx.SaftyMasterTime = DateTime.Now;
                            cx.DeptMasterId = int.Parse(TfDept_Master_ID.SelectedItem.Value);
                            cx.BossUserID = int.Parse(TfBoss_ID.SelectedItem.Value);
                        }
                        //禁止公司领导审批框
                        if ((Cookies.getCookies("cDeptID") == "1") && Cookies.getCookies("cLevelID") == "1")
                        {
                            cx.BossRemark = TfBoss_Remark.Text;
                            cx.BossStatus = int.Parse(TfBoss_Status.SelectedItem.Value.ToString());
                            cx.BossTime = DateTime.Now;
                        }
                        //禁止部门领导审批框
                        if ((Cookies.getCookies("cDeptID") == getDeptId(int.Parse(TfDeptID.Value.ToString())).ToString()) && Cookies.getCookies("cLevelID") == "1")
                        {
                            cx.DeptMasterRemark = TfDept_Master_Remark.Text;
                            cx.DeptMasterStatus = int.Parse(TfDept_master_status_Comb.SelectedItem.Value.ToString());
                            cx.DeptMasterTime = DateTime.Now;
                        }
                        cx.Save();
                        Clear();
                        Err.Warning("批复成功！",MessageBox.Icon.NONE);
    }
    protected void Clear()
    {
        //清空表单
        FormPanel1.Title = "员工诚信档案记录表";
        TfDeptName.Text = "";
        TfUserId.Text = "";
        TfDeptID.Value = "";

        rID.Value = "";
        IDLabel.Text = "";
        TfUserName.Text = "";
        TfUserId.Text = "";
        TfEntering_username.Text = "";

        TfBoss_ID.Reset();
        TfDept_Master_ID.Reset();
        TfAccident_reason.Text = "";
        TfSafty_Master_Status.Reset();
        TfAccident_time.Text = "";
        TfAccident_Duty.Text = "";
        tffileshow.Text = "";
        TfDept_Master_Remark.Text = "";
        TfBoss_Status.Reset();
        TfSafty_master_remark.Text = "";
        TfDept_master_status_Comb.Reset();
        TfBoss_Remark.Text = "";
    }
    protected void Store1Bind()
    {
        Query q = new Query(VwStaffSaftyStep1.Schema);
        q.SetSelectList(
            "id as SID,EN_Username,EN_Department,Dept_Master_Status,Boss_Status,Safty_Master_Status");
        if ((Cookies.getCookies("cDeptID") != "6") && (Cookies.getCookies("cDeptID") != "1"))
        {
            q.AddWhere("Dept_master_ID", Cookies.getCookies("cUid"));
            q.AddWhere("safty_master_status", 1);
            q.AddWhere("dept_master_status", 0);
        }
        if (Cookies.getCookies("cDeptID") == "1")
        {
            q.AddWhere("Boss_UserID", Cookies.getCookies("cUid"));
            q.AddWhere("safty_master_status", 1);
            q.AddWhere("dept_master_status", 1);
            //q.AddWhere("boss_status",SubSonic.Comparison.NotEquals,"2");
        }
        if (Cookies.getCookies("cDeptID") == "6")
        {
            q.AddWhere("Safty_MasterID", Cookies.getCookies("cUID"));
            q.AddWhere("safty_master_status", SubSonic.Comparison.NotEquals, 2);
        }
        Store1.DataSource = q.ExecuteReader();
        Store1.DataBind();
    }
    protected void Store3Bind()
    {
        int deptId = int.Parse(TfDeptID.Value.ToString());
        if (deptId > 10)
        {
            Query q = new Query(CxSysDept.Schema);
            q.SetSelectList("deptid");
            q.AddWhere("ID", deptId);
            deptId = int.Parse(q.ExecuteScalar().ToString());
        }
        Query q2 = new Query(CxUserBase.Schema);
        q2.SetSelectList("username,uID");
        q2.AddWhere("levelid", 1);
        q2.AddWhere("depid", deptId);
        Store3.DataSource = q2.ExecuteDataSet();
        Store3.DataBind();

    }
    protected int getDeptId(int deptId)
    {
        if (deptId > 10)
        {
            Query q = new Query(CxSysDept.Schema);
            q.SetSelectList("deptid");
            q.AddWhere("ID", deptId);
            deptId = int.Parse(q.ExecuteScalar().ToString());
        }
        return deptId;
    }
}
